// 公共方法，属性，静态工具
// modal父类
// 对每种状态内部属性加工，或者是每种状态的扩展
// SUCCESS WARNING ERROR Modal

// 有一个工厂通过传入的状态给我自动实例化对应的类
// modal Factory

import { ModalTypes, ModalClassName } from "./typing.js";

// 主类用于处理公共部分的
class Modal {
    constructor(status) {
        this.status = status;
    }

    get className() {
        let classStr = 'modal';
        switch (this.status) {
            case ModalTypes.SUCCESS:
                classStr += ' ' + ModalClassName.SUCCESS;
                break;
            case ModalTypes.WARNING:
                classStr += ' ' + ModalClassName.WARNING;
                break;
            case ModalTypes.ERROR:
                classStr += ' ' + ModalClassName.ERROR;
                break;
            default:
                break
        }
        return classStr;
    }

    static checkStatusIsExist(types, status) {
        for (let k in types) {
            if (types[k] === status) {
                return true;
            }
        }

        return false;
    }

    static outputInfo(info) {
        console.log(info);
    }
}

// 三个状态都是集成Modal的，并且扩展的方法都可以写这里，然后在create中return回去使用
class SuccessModal extends Modal {
    constructor(title) {
        super(ModalTypes.SUCCESS);
        this.title = '成功:' + title;
    }

    goHome (url) {
        window.location.href = url;
    }
}

class WarningModal extends Modal {
    constructor(title) {
        super(ModalTypes.WARNING);
        this.title = '警告:' + title;
    }

    outputInfo (info) {
        Modal.outputInfo(info);
    }
}

class ErrorModal extends Modal {
    constructor(title) {
        super(ModalTypes.ERROR);
        this.title = '错误:' + title;
    }

    outputInfo (err) {
        Modal.outputInfo(err);
    }
}

// 加工方法
class ModalFactory {
    constructor(dom) {
        this.dom = dom;
    }

    // 就是外面使用的方法
    create(title, status) {
        const statusIsExist = Modal.checkStatusIsExist(ModalTypes, status);

        if (!statusIsExist) {
            throw new Error('Modal type is inCorrect.');
        }

        const dom = this.dom;
        let modal = null;
        switch (status) {
            case ModalTypes.SUCCESS:
                modal = new SuccessModal(title);
                break;
            case ModalTypes.WARNING:
                modal = new WarningModal(title);
                break;
            case ModalTypes.ERROR:
                modal = new ErrorModal(title);
                break;
            default:
                break
        }
        dom.getElementsByTagName('header')[0].innerText = modal.title;
        dom.className = modal.className;

        return {
            outputInfo: modal.outputInfo,
            goHome: modal.goHome
        }
    }
}

export default ModalFactory;